/**
 * 
 */
package org.atmaca.ert.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;

import org.atmaca.ert.model.Feed;
import org.springframework.stereotype.Repository;

/**
 * @author "huseyin_atmaca"
 *
 */

@Repository
public class DbDaoImpl {
	
	@PersistenceContext
	private EntityManager em;
	
	public boolean isItemExists(Feed feed){
		Query query = em.createQuery("SELECT f FROM FEEDS f WHERE f.uri = :uri");
		query.setParameter("uri", feed.getUri());
		
		List<Feed> resultList = (List<Feed>) query.getResultList();
		
		return (resultList.size()==0) ? false : true;
	}
	
	public void saveItem(Feed feed){
		em.persist(feed);
	}
	
	public List<Feed> getAll(){
		Query query = em.createQuery("SELECT f FROM FEEDS f");
		return (List<Feed>)query.getResultList();
	}
	
	public List<Feed> findLastNFeeds(Integer maxResult){
		Query query = em.createQuery("SELECT f FROM FEEDS f");
		query.setMaxResults(maxResult);
		return (List<Feed>)query.getResultList();
		
	}

}
